#include "hunString.h"
#include "stdio.h"
#include "string.h"

// -------------------------------------------------------------
hunInt hunString_Size(const hunString s)
{
    return strlen(s);
}
// -------------------------------------------------------------
hunInt hunString_Begin(const hunString s)
{
    int i;
    int size = strlen(s);
    for(i=0;i<size;++i)
    {
        if(s[i] == ' ')
            continue;
        return i;
    }
    return i;
}
// -------------------------------------------------------------
hunInt hunString_End(const hunString s)
{
    int i;
    for(i=strlen(s); --i;)
    {
        if( s[i] == ' ' || s[i] == '\n' || s[i] == '\0' )
            continue;
        return i;
    }
    return i;
}
// -------------------------------------------------------------
hunInt hunString_Length(const hunString s)
{
    return strlen(s);
}
// -------------------------------------------------------------
hunInt hunString_Compare(const hunString a, const hunString b)
{
    const hunChar *pa = a;
    const hunChar *pb = b;
    while(1)
    {
        if(*pa == '\0' && *pb == '\0')
            return 0;
        else if(*pa == '\0')
            return -1;
        else if(*pb == '\0')
            return 1;
        if( *pa < *pb)
            return -1;
        else if(*pa > *pb)
            return 1;
        pa++; pb++;
    }
    return 0;
}
// -------------------------------------------------------------
// find if string a is in s, >=0 (found)   <0  (not found)
hunInt hunString_Find(hunString s, hunString a)
{
    hunInt i;
    hunInt s_len = strlen(s);
    hunInt a_len = strlen(a);
    hunChar fst_char = a[0];
    hunString tmp = (hunString) hunCalloc(a_len+1);
    if(!tmp)
        return 0;
    for(i=0;i<s_len;++i)
    {
        // find first match
        if(s[i] == fst_char)
        {
            strncpy(tmp, &s[i], a_len);
            if(hunString_Compare( a, tmp ) == 0)
            {
                hunFree(tmp);
                return i;
            }
            continue;
        }
    }
    hunFree(tmp);
    return -1;
}
// -------------------------------------------------------------
hunInt hunString_ToInt(const hunString s)
{
    hunInt int_data;
    sscanf(s, "%d", &int_data);
    return int_data;
}
// -------------------------------------------------------------
hunFloat hunString_ToFloat(const hunString s)
{
    hunFloat float_data;
    sscanf(s, "%g", &float_data);
    return float_data;
}
// -------------------------------------------------------------
hunDouble hunString_ToDouble(const hunString s)
{
    hunDouble double_data;
    sscanf(s, "%g", &double_data);
    return double_data;
}
// -------------------------------------------------------------

hunString hunString_ToString(const hunInt i, hunString s)
{
    //itoa( i, s, 10 );
    if( !s )
        s = hunMalloc( 16 );
    sprintf(s, "%d", i);
    return s;
}

// -------------------------------------------------------------
hunString hunString_Reset(hunString s)
{
    memset(s, 0 , strlen(s) );
    return s;
}
// -------------------------------------------------------------
hunString hunString_Append(hunString s, hunString a)
{
   return s; 
}
// -------------------------------------------------------------
hunString hunString_Pushback(hunString s, hunString a)
{
    return hunString_Append(s,a);
}
// -------------------------------------------------------------
hunString hunString_Reverse(hunString s)
{
    return s;
}
// -------------------------------------------------------------
hunString hunString_Clear(hunString s)
{
    s = hunString_Reset(s);
    return s;
}
// -------------------------------------------------------------

hunBool hunString_Empty(const hunString s)
{
    int i, size;
    if(!s)
        return hunTrue;
    size = strlen(s);
    for(i=0;i<size;++i)
        if( s[i] != 0 || s[i] != '\0')
            return hunFalse;
    return hunTrue;
}
// -------------------------------------------------------------
hunBool hunString_Copy(hunString src, hunString dsc);
// -------------------------------------------------------------

hunChar hunString_At(hunString s, hunInt idx)
{
    if(idx < 0 || idx >= strlen(s) )
        return (hunChar)0;
    return s[idx];
}
// -------------------------------------------------------------

void hunString_Replace(hunString s, hunString a, hunString b); // replace a to b
// -------------------------------------------------------------
